package cn.com.dcsgo.library.admin.controller;

import cn.com.dcsgo.library.admin.domain.dto.PageDTO;
import cn.com.dcsgo.library.admin.domain.po.Book;
import cn.com.dcsgo.library.admin.domain.query.BookPageQuery;
import cn.com.dcsgo.library.admin.domain.request.AddBookCopiesRequest;
import cn.com.dcsgo.library.admin.domain.request.UpdateBookRequest;
import cn.com.dcsgo.library.admin.domain.vo.BookBorrowedOverviewVO;
import cn.com.dcsgo.library.admin.domain.vo.BookVO;
import cn.com.dcsgo.library.admin.domain.request.SaveBookRequest;
import cn.com.dcsgo.library.admin.response.ResponseResult;
import cn.com.dcsgo.library.admin.service.IBooksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 图书相关接口
 *
 * @author Dcsgo
 * @since 2024-09-02
 */
@RestController
@RequestMapping("/books")
@Api(tags = "图书相关接口")
@RequiredArgsConstructor
@Slf4j
public class BooksController {
    private final IBooksService booksService;

    @GetMapping("page")
    @ApiOperation("分页获取图书概览信息")
    public ResponseResult<PageDTO<BookBorrowedOverviewVO>> booksOverviewByPage(BookPageQuery pageQuery) {
        PageDTO<BookBorrowedOverviewVO> pageDTO = booksService.booksOverviewByPage(pageQuery);
        return ResponseResult.success(pageDTO);
    }

    @PostMapping
    @ApiOperation("添加图书")
    public ResponseResult<Long> saveBook(@RequestBody SaveBookRequest saveBookRequest) {
        try {
            return ResponseResult.success(booksService.save(saveBookRequest));
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (RuntimeException re) {
            log.error(re.getMessage());
            return ResponseResult.fail("后端异常，请联系管理人员！");
        }
    }

    @GetMapping("{id}")
    @ApiOperation("通过图书编号获取图书详细信息")
    public ResponseResult<BookVO> queryBookById(@PathVariable Long id) {
        try {
            return ResponseResult.success(booksService.queryBookDetails(id));
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(null, iae.getMessage());
        }
    }

    @PutMapping
    @ApiOperation("修改图书")
    public ResponseResult<Book> updateBook(@RequestBody UpdateBookRequest request) {
        try {
            return ResponseResult.success(booksService.updateBook(request));
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (RuntimeException re) {
            log.error(re.getMessage());
            return ResponseResult.fail("后端异常，请联系管理人员！");
        }
    }

    @PostMapping("copies")
    @ApiOperation("添加图书复本")
    public ResponseResult<? extends Serializable> addBookCopies(@RequestBody  AddBookCopiesRequest request) {
        try {
            booksService.addBookCopies(request);
            return ResponseResult.success();
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (RuntimeException re) {
            log.error(re.getMessage());
            return ResponseResult.fail("后端异常，请联系管理人员！");
        }
    }

    @DeleteMapping("copies")
    @ApiOperation("下架图书复本")
    public ResponseResult<? extends Serializable> removeBookCopies(@RequestParam("copiesIds")List<Long> copiesIds) {
        try {
            booksService.removeBookCopies(copiesIds);
            return ResponseResult.success();
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (RuntimeException re) {
            log.error(re.getMessage());
            return ResponseResult.fail("后端异常，请联系管理人员！");
        }
    }

    @DeleteMapping()
    @ApiOperation("下架图书")
    public ResponseResult<? extends Serializable> removeBooks(@RequestParam("bookIds")List<Long> bookIds) {
        try {
            booksService.removeBooks(bookIds);
            return ResponseResult.success();
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (RuntimeException re) {
            log.error(re.getMessage());
            return ResponseResult.fail("后端异常，请联系管理人员！");
        }
    }
}
